//
//  WelcomeView.swift
//  VVbo
//
//  Created by Mr.Run on 2017/8/28.
//  Copyright © 2017年 Mr.Run. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeView: UIView {
    
    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var tipLabel: UILabel!
    @IBOutlet weak var bottomCons: NSLayoutConstraint!
    
    class func welcomeView()->WelcomeView {
        let nib = UINib(nibName: "WelcomeView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WelcomeView
        v.frame = UIScreen.main.bounds
        
        return v
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        guard let urlString = NetworkManager.shared().userAccount.avatar_large,
            let url = URL(string:urlString)
            else {
            return
        }
        
        iconView.sd_setImage(with: url, placeholderImage: UIImage(named:"avatar_default_big"))
        
        iconView.layer.cornerRadius = iconView.bounds.size.width * 0.5
        iconView.layer.masksToBounds = true
        
    }
    
    override func didMoveToWindow() {
        super.didMoveToWindow()
//        self.layoutIfNeeded()
        UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            self.bottomCons.constant = self.bounds.size.height - 200
            self.layoutIfNeeded()
        }) { (_) in
            UIView.animate(withDuration: 0.3, animations: {
                self.tipLabel.alpha = 1.0
            }, completion: { (_) in
                UIView.animate(withDuration: 0.7, delay: 0.7, options: [], animations: {
                    self.alpha = 0
                }, completion: { (_) in
                    self.removeFromSuperview()
                })
            })
        }
    }
}
